package com.yt.order.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import com.yt.order.models.service.IDispatchRuleFilterService;
import com.yt.order.models.bo.DispatchRuleFilterBO;
import com.yt.order.models.dto.DispatchRuleFilterDTO;
import com.yt.order.models.bo.DispatchRuleFilterSaveBO;

import java.util.List;

/**
 * 派单条件过滤信息控制器
 *
 * @author yzz
 * @Date 2025-09-03 14:18:02
 */

@RestController
@RequestMapping("/api/dispatchRuleFilter")
@Slf4j
@Api(value = "DispatchRuleFilterApiController",tags = "派单条件过滤信息Api接口")
public class DispatchRuleFilterApiController {

    @DubboReference
    private IDispatchRuleFilterService dispatchRuleFilterService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<DispatchRuleFilterDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        DispatchRuleFilterDTO dto = dispatchRuleFilterService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "派单条件过滤信息列表",notes = "派单条件过滤信息列表")
    @PostMapping("/queryPageList")
    public Result<Page<DispatchRuleFilterDTO>> queryPageList(DispatchRuleFilterBO bo){
        Wrapper<DispatchRuleFilterDTO> wrapper = new EntityWrapper<>();
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<DispatchRuleFilterDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = dispatchRuleFilterService.selectPage(page, wrapper);

        return new Result<>(page);
    }

    @ApiOperation(value = "获取所有派单条件过滤信息", notes = "获取所有派单条件过滤信息")
    @PostMapping("/listAll")
    public Result<List<DispatchRuleFilterDTO>> listAll() {
        List<DispatchRuleFilterDTO> list = dispatchRuleFilterService.selectList(null);
        return new Result<>(list);
    }
    
    @ApiOperation(value = "添加派单条件过滤信息",notes = "添加派单条件过滤信息")
    @PostMapping(value = "/add")
    public Result insert(DispatchRuleFilterSaveBO saveBO) {
        boolean flag = dispatchRuleFilterService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "修改派单条件过滤信息",notes = "修改派单条件过滤信息")
    @PostMapping("/update")
    public Result update(DispatchRuleFilterSaveBO saveBO){
        DispatchRuleFilterDTO dto = new DispatchRuleFilterDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = dispatchRuleFilterService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }



}
